package com.sky.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.BeanUtils.MyBeanUtils;
import com.sky.QueryByPage.CategoryQueryByPage;
import com.sky.dto.CategoryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.vo.CategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceimpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageResult getByPage(CategoryQueryByPage cqbp) {
        PageHelper.startPage(cqbp.getPage(),cqbp.getPageSize());
        List<Category> categories =  categoryMapper.getByPage(cqbp);
        List<CategoryVO> categoryVOS = MyBeanUtils.ListBeanUtils(categories, CategoryVO::new);
        Page<CategoryVO> p = (Page) categories;
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.update(category);
    }

    @Override
    public void enable(Integer status, Integer id) {
        categoryMapper.enable(status,id);
    }

    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.add(category);
    }

    @Override
    public void delete(Integer id) {
        categoryMapper.delete(id);
    }

    @Override
    public List<CategoryVO> getlist(Integer id) {
        List<Category> category =  categoryMapper.getlist(id);
//        List<CategoryVO> categoryVO = new CategoryVO();
        List<CategoryVO> categoryVOS = MyBeanUtils.ListBeanUtils(category, CategoryVO::new);
//        BeanUtils.copyProperties(category,categoryVO);
        return categoryVOS;
    }
}
